// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
'use strict';


const ffi = require('ffi');
const path = require('path');

const libSunVoteSDK = ffi.Library(path.join(__dirname, 'dll/TopicReader.dll'), {
  "TPEDU_Init": ["int", []],//动态库初始化
  "TPEDU_Release": ["int", []],//动态库释放
  "TPEDU_SetCallbackAddr": ["int", ["pointer"]],//设置回调函数
  "TPEDU_EnumDevices": ["int", ["string"]],//设备枚举
  "TPEDU_OpenDevice": ["int", ["string"]],//打开设备
  "TPEDU_AddtoWhitelist": ["int", ["int","string","int"]],//添加白名单
  "TPEDU_SetWorkMode": ["int", ["int","int","string","int"]],//设置考试模式
  "TPEDU_Start": ["int", ["int"]],//开始答题
});

var init = libSunVoteSDK.TPEDU_Init();
console.log(init);

var NameList = new Buffer(4096);

var en = libSunVoteSDK.TPEDU_EnumDevices(NameList);
console.log(en);

NameList = NameList.toString();
NameList = NameList.split(";");

console.log(NameList);

var device = '';
for (var i = 0; i < NameList.length; i++) {

  if(NameList[i] != "") {
    device = NameList[i];
    console.log(NameList[i]);
    break;
  }
}

console.log(device);

function _CallBack(dev, msg, param1, param2) {
  console.log("HDParamEventCallBack--:"+"dev :"+dev +" msg:"+ msg + " param1:" +param1 + " param2:" +param2);
}
var SetCallbackAddr = ffi.Callback('void',['int','int','int', 'string'],_CallBack);

libSunVoteSDK.TPEDU_SetCallbackAddr(SetCallbackAddr);



var DeviceHandle = libSunVoteSDK.TPEDU_OpenDevice(device);
console.log(DeviceHandle);

var white = libSunVoteSDK.TPEDU_AddtoWhitelist(DeviceHandle, '1413069546;1414902762;0338009587', 1000);
console.log(white);

var s3 = libSunVoteSDK.TPEDU_SetWorkMode(DeviceHandle, 6, "0", 1000);    //设置答题模式
console.log(s3);
var s4 = libSunVoteSDK.TPEDU_Start(DeviceHandle);
console.log(s4);




















